package com.kylin.dal.entity.user;

import com.kylin.shared.common.entity.EntryRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户职级数据仓库
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月05日 11:22
 */
@Repository
public interface UserWechatRepository extends EntryRepository<UserWechatEntry, Long> {

    /**
     * 更新微信用户信息（从微信返回的用户信息）
     * @param entry 用户信息数据对象
     * @return 执行成功影响的数据量
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update UserWechatEntry w set w.gender=:#{#entry.gender},w.nickName=:#{#entry.nickName}," +
            "w.province=:#{#entry.province},w.city=:#{#entry.city},w.country=:#{#entry.country}," +
            "w.avatarUrl=:#{#entry.avatarUrl},w.language=:#{#entry.language},w.updatedAt=:#{#entry.updatedAt}," +
            "w.updatedBy=:#{#entry.updatedBy} where w.userId=:#{#entry.userId}")
    Integer updateWechatUserInfo(@Param("entry")UserWechatEntry entry);

    /**
     * 更新用户手机号
     * @param entry 用户信息数据对象
     * @return 执行成功影响的数据量
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update UserWechatEntry w set w.phoneNumber=:#{#entry.phoneNumber} where w.userId=:#{#entry.userId}")
    Integer updatePhone(@Param("entry")UserWechatEntry entry);
}
